/* 
 * Copyright (C) 2007 Google Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.google.android.samples.app;

// Need the following import to get access to the app resources, since this
// class is in a sub-package.
import com.google.android.samples.R;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.Service;
import android.content.Intent;
import android.os.BinderNative;
import android.os.IBinder;
import android.os.Parcel;
import android.widget.RemoteViews;

/**
 * This is an example of service that will update its status bar balloon 
 * every 5 seconds for a minute.
 * 
 */
public class NotifyingService extends Service implements Runnable {
    
    // Use a layout id for a unique identifier
    private static int MOOD_NOTIFICATIONS = R.layout.status_bar_notifications;
    
    @Override
    protected void onCreate() {
        mNM = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

        // Start up the thread running the service.  Note that we create a
        // separate thread because the service normally runs in the process's
        // main thread, which we don't want to block.
        Thread thr = new Thread(null, this, "NotifyingService");
        thr.start();
    }

    @Override
    protected void onDestroy() {
        // Cancel the persistent notification.
        mNM.cancel(MOOD_NOTIFICATIONS);
    }

    public void run() {
        
        try {
            for (int i = 0; i < 4; ++i) {
                Thread.sleep(5 * 1000);
                setMoodRemoteView(R.drawable.stat_happy,
                        R.string.status_bar_notifications_happy_message);
                Thread.sleep(5 * 1000);
                setMoodRemoteView(R.drawable.stat_neutral,
                        R.string.status_bar_notifications_ok_message);
                Thread.sleep(5 * 1000);
                setMoodRemoteView(R.drawable.stat_sad,
                        R.string.status_bar_notifications_sad_message);
            }
        } catch (Exception ex) {
        }
        // Done with our work...  stop the service!
        this.stopSelf();
    }

    @Override
    public IBinder getBinder() {
        return mBinder;
    }
    
    private void setMoodRemoteView(int moodId, int textId) {
        String str = getResources().getString(textId);
        RemoteViews remoteViews = new RemoteViews(this.getPackageName(),
                R.layout.status_bar_balloon);
        remoteViews.setImageViewResource(R.id.icon, moodId);
        remoteViews.setTextViewText(R.id.text, str);

        Notification notification = new Notification(moodId, str, null, null, remoteViews);
        mNM.notify(MOOD_NOTIFICATIONS, notification);
    }

    // This is the object that receives interactions from clients.  See
    // RemoteService for a more complete example.
    private final IBinder mBinder = new BinderNative() {
        @Override
        protected boolean onTransact(int code, Parcel data, Parcel reply, int flags) {
            return super.onTransact(code, data, reply, flags);
        }
    };

    private NotificationManager mNM;
}
